//
//  GameViewModel.swift
//  DouYuZB
//
//  Created by 中创 on 2020/4/24.
//  Copyright © 2020 梁森. All rights reserved.
//

import UIKit

class GameViewModel {
    /// 所有的游戏数据
    var allGameData:[GameModel] = []
    
}

extension GameViewModel{
    /// 请求所有的游戏数据
    func requestAllGameData(finishCallBack:@escaping() -> (), errorCallBack:@escaping()->()){ 
        let parameters = ["":""]
        NetworkTool.requestData(url: "http://capi.douyucdn.cn/api/v1/getColumnDetail", params: parameters, method: .GET, success: { (response) in
            guard let responseDict = response as? [String:NSObject] else{return}
            guard let dataArray = responseDict["data"] as? [[String:NSObject]] else{return}
            for dict in dataArray{
                let model = GameModel.deserialize(from: dict)
                self.allGameData.append(model!)
                if self.allGameData.count > 30 {
                    break
                }
            }
            print("所有游戏返回：数据\(self.allGameData.toJSONString() ?? "")")
            finishCallBack()
        }) { (response) in
            errorCallBack()
        }
    }
}
